// index.js
import {
  GET_CHACK_LIST,
  GET_SELF_LIST,
  GET_USERINFO,
  GET_CHACK_PERIODS
} from '../../request/api'
let app = getApp()
Page({
  data: {
    userInfo: {},
    list: [],
    keyword: '',
    page: 1,
    last_page: 100,
    fin: false,
    loading: false,
    curStatus: '',
    curPeriods: '',
    curYear: "",
    yearsMap: [],
    statusMap: [
      { text: '全部', value: '' },
      { text: '待督查', value: 1 },
      { text: '已督促', value: 3 },
    ],
    periodsMap: [
      { text: '全部', value: '' },
    ]
  },
  tabChangePeriods(e) {
    this.setData({
      curPeriods: e.detail
    })
    this.getList(1)
  },
  tabChangeStatus(e) {
    this.setData({
      curStatus: e.detail
    })
    this.getList(1)
  },
  tabChangeYears(e) {
    this.setData({
      curYear: e.detail
    })
    this.getList(1)
  },
  //获取最近年份
  getNearYears(n, m) {
    if (m === '' || m === undefined)
      m = 0;
    var myDate = new Date();
    var startYear = myDate.getFullYear() - n;//起始年份 
    var endYear = myDate.getFullYear() + m;//结束年份
    var arr = new Array();
    for (var i = startYear; i <= endYear; i++) {
      var obj = { "value": i, "text": i + "年" };
      arr.push(obj);
    }
    return arr;
  },
  getUser(cb) {
    GET_USERINFO().then(res => {
      this.setData({
        userInfo: res
      })
      app.globalData.userInfo = res
      cb && cb()
    })
  },
  searchData() {
    this.getList(1)
  },
  getList(page = 1) {
    let data = {
      currentPage: page,
      pageSize: 20,
      targetName: this.data.keyword,
      status: this.data.curStatus,
      inspectBatch: this.data.curPeriods,
      year: this.data.curYear,
    }
    this.setData({
      loading: true
    })
    let datalist = page == 1 ? [] : this.data.list;
    if (page == 1) {
      this.setData({
        page: 1,
        fin: false
      })
    }
    GET_CHACK_LIST(data).then(res => {
      this.setData({
        list: [
          ...datalist,
          ...res.dataList
        ],
        loading: false,
        last_page: res.page.totalPage,
      })
    })
  },
  toPageDes(item) {
    let id = item.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/des/des?id=${id}`,
    })
  },
  //获取批次
  getPeriods() {
    GET_CHACK_PERIODS().then(res => {
      let arr = [{ text: '全部', value: '' }]
      for (let i in res) {
        arr.push({
          text: res[i],
          value: res[i]
        })
      }
      this.setData({
        periodsMap: arr
      })
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page > this.data.last_page) {
      this.setData({
        fin: true
      })
      return false
    }
    this.setData({
      page: this.data.page + 1,
      fin: false
    })
    this.getList(this.data.page)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      list: []
    })
    this.getList()
  },
  onLoad(options) {
    this.getPeriods()
    this.getUser(() => {
      this.getList()
    })
    let arr = this.getNearYears(5, 1)
    this.setData({
      yearsMap: [
        { text: '全部', value: '' },
        ...arr
      ]
    })
  }
})